[AdminUi] Add badge field component with enum support#308
[AdminUi] Add badge field component with enum support#308AlexandreBulete wants to merge 5 commits intoSylius:mainfrom
Conversation
loic425
left a comment
There was a problem hiding this comment.
Thx for your proposal. It's an interesting idea.
1/ You Twig template has too much complexity
2/ test_html_attribute placed on color is strange
3/ magic with getLabel, getColor without any interface is not cool.
|
Thanks for the feedback! I've refactored the badge implementation to address all concerns:
Created proper architecture with value objects and Twig extension. Documentation included. Let me know if you need any changes! |
| * This class normalizes badge data from various sources (BadgeableInterface, arrays, strings) | ||
| * into a consistent format for rendering. | ||
| */ | ||
| final readonly class BadgeData |
There was a problem hiding this comment.
Probably a nit-pick but shoudln't it simply be called Badge?
There was a problem hiding this comment.
Well, for me, this class is more akin to DTO behaviour, which involves instantiating and formatting data (hydrated from the enum) to be used properly by the twig template. That's why it seemed more logical to me to add something like the suffix “Data.”
| {% set badgeData = sylius_badge_data(data, options|default({})) %} | ||
|
|
||
| {% if badgeData %} | ||
| <span class="badge rounded-pill text-{{ badgeData.color }}" {{ sylius_test_html_attribute('badge', badgeData.value ?? badgeData.label) }}> |
There was a problem hiding this comment.
I think the color should be on the badge itself (background instead of text), so for instance a colour like bg-purple-lt would be rendered via
| <span class="badge rounded-pill text-{{ badgeData.color }}" {{ sylius_test_html_attribute('badge', badgeData.value ?? badgeData.label) }}> | |
| <span class="badge rounded-pill bg-{{ badgeData.color }}" {{ sylius_test_html_attribute('badge', badgeData.value ?? badgeData.label) }}> |
There was a problem hiding this comment.
Yes! After testing it, you've convinced me! :)
I'll remove the rounded-pill and add this CSS class bg-{color}-lt instead of a simple text-color to match your screenshot. I also fixed the icon-text flex alignment too.
There was a problem hiding this comment.
Would it be possible globally for all those kind of templates to get a way to expose all HTML attributes the same way Symfony form block "attributes" is working?
It's just an idea, it's not required a required change for this PR.

Description
This PR adds a new badge field component for Sylius Grid that supports:
getLabel(),getColor(), andgetIcon()methodsoptions.vars.colors,labels, andiconsrounded-pillUsage Example
With PHP Enum
Files added
templates/shared/grid/field/badge.html.twigtemplates/shared/helper/field/badge.html.twig